<template>
  <el-dialog
    :close-on-click-modal="false"
    title="批量添加"
    v-model="visible"
    align-center
    width="600"
    destroy-on-close
  >
    <el-form
      ref="formRef"
      :model="form"
      label-position="top"
      :label-width="140"
    >
      <el-form-item
        label="可查看供应商"
        class="w-full"
        prop="relation_data"
        :rules="[
          { required: true, message: '不能为空', trigger: 'relation_data' },
        ]"
      >
        <VSelect
          v-model="form.relation_data"
          placeholder="供应商"
          type="supplier"
          multiple
          collapse-tags
          clearable
          filterable
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose(false)">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="save"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>
<script setup lang="jsx">
import { ElMessage } from "element-plus";
import { batchAddSupplier } from "../../api.js";
const emit = defineEmits(["close", "success"]);
const loading = ref(false);
const visible = ref(false);
const form = ref({});
const open = async (fa_ids) => {
  visible.value = true;
  form.value.fa_ids = fa_ids;
};

const formRef = ref();
const save = async () => {
  const validator = await formRef?.value?.validate();
  if (!validator) return ElMessage.warning("必填项不能为空!");
  try {
    loading.value = true;
    const { code, message } = await batchAddSupplier(form.value);
    ElMessage.success("操作成功!");
    handleClose(true);
  } catch (error) {
  } finally {
    loading.value = false;
  }
};
const handleClose = (refresh) => {
  visible.value = false;
  form.value = {};
  formRef?.value?.resetFields();
  emit("close");
  refresh && emit("success");
};
defineExpose({ open });
</script>
<style lang="scss" scoped></style>
